package nuc.soft.A01.jzs.controller;

import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import nuc.soft.A01.jzs.model.Role;
import nuc.soft.A01.jzs.model.general.GeneralUser;
import nuc.soft.A01.jzs.model.user.Admin;
import nuc.soft.A01.jzs.services.RoleService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/role")
public class RoleController {

	@Autowired
	private RoleService service;

	@RequestMapping("/showall")
	public String getAllrole(HttpServletRequest request,
			HttpServletResponse response) {
//		HttpSession session = request.getSession();
//		GeneralUser user = (GeneralUser) session.getAttribute("user");
//		
//		if (user != null && user.getRoleid().equals("1")) {
			
			ArrayList<Role> allrole = service.getAllRole();
			request.setAttribute("roles", allrole);
			
			return "role/roleindex";
//		}
//		return "index";
	}


	@RequestMapping("/showadd")
	public String showadd(HttpServletRequest request,
			HttpServletResponse response) {

//		HttpSession session = request.getSession();
//		GeneralUser user = (GeneralUser) session.getAttribute("user");
//		if (user != null && user.getRoleid().equals("1")) {
//			System.out.println("showadd");
			return "role/add";
//		}
//		return "index";
	}

	@RequestMapping("/add")
	public String add(HttpServletRequest request, HttpServletResponse response,
			Role role) {
//		HttpSession session = request.getSession();
//		GeneralUser user = (GeneralUser) session.getAttribute("user");
//		if (user != null && user.getRoleid().equals("1")) {
			service.insert(role);
			return "forward:/role/showall";
//		}
//		return "index";
	}

	@RequestMapping("/showedit")
	public String showedit(HttpServletRequest request,
			HttpServletResponse response, String theid) {
		HttpSession session = request.getSession();
		GeneralUser user = (GeneralUser) session.getAttribute("user");
		if (user != null && user.getRoleid().equals("1")) {
			Role role = service.getARoleById(theid);
			request.setAttribute("editrole", role);
			return "role/edit";
		}
		return "index";
	}

	@RequestMapping("/edit")
	public String edit(HttpServletRequest request,
			HttpServletResponse response, Role role) {
//		HttpSession session = request.getSession();
//		GeneralUser user = (GeneralUser) session.getAttribute("user");
//		if (user != null && user.getRoleid().equals("1")) {
			service.update(role);
			return "forward:/role/showall";
//		}
//		return "index";
	}
}
